package com.platform.controller;

import com.platform.entity.LogisticsEntity;
import com.platform.service.LogisticsService;
import com.platform.service.OrderService;
import com.platform.utils.AliSmsUtil;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 物流表Controller
 *  订单管理:发货管理
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2019-02-25 20:27:31
 */
@RestController
@RequestMapping("logistics")
public class LogisticsController {
    @Autowired
    private LogisticsService logisticsService;
    @Autowired
    private OrderService orderService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("logistics:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<LogisticsEntity> logisticsList = logisticsService.queryList(query);
        int total = logisticsService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(logisticsList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("logistics:info")
    public R info(@PathVariable("id") Integer id) {
        LogisticsEntity logistics = logisticsService.queryObject(id);

        return R.ok().put("logistics", logistics);
    }

    /**
     * 保存
     */
    @Ignore
    @RequestMapping("/save")
//    @RequiresPermissions("logistics:save")
    public R save(@RequestBody LogisticsEntity logistics) {
        boolean result =logisticsService.check(logistics.getOrderno());
        if(result){
            return R.error("订单已发货，不要重复发货");
        }
        logisticsService.save(logistics);
        orderService.confirm(logistics.getOrderno() ,logistics.getLogisticsNo());
        Map<String,Object> map = logisticsService.querySmsTemplate(11);
        if(map!=null){
           AliSmsUtil.sendSms(logistics.getPhone(), map.get("content").toString(), "", logistics.getLogisticsNo());
        }
        return R.ok();
    }
    /**
     * 保存
     */
    @Ignore
    @RequestMapping("/saveSpc")
//    @RequiresPermissions("logistics:save")
    public R saveSpc(@RequestBody LogisticsEntity logistics) {
        boolean result =logisticsService.check(logistics.getOrderno());
        if(result){
            return R.error("订单已发货，不要重复发货");
        }
        logisticsService.save(logistics);
        orderService.confirm(logistics.getOrderno() ,logistics.getLogisticsNo());
        return R.ok();
    }
    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("logistics:update")
    public R update(@RequestBody LogisticsEntity logistics) {
        logisticsService.update(logistics);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("logistics:delete")
    public R delete(@RequestBody Integer[] ids) {
        logisticsService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<LogisticsEntity> list = logisticsService.queryList(params);

        return R.ok().put("list", list);
    }
}
